/* 
 * Copyright (C) 2007 - 2010 Amerika Labs <http://amerika-labs.com/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

#include "ScriptPCH.h"
#include "ScriptMgr.h"
#include "Creature.h"
#include "CreatureEventAI.h"
#include <time.h>

enum texts
{
	NPC_CONNIE		= 8000003,
	NPC_VOLKERDIE	= 8000004,

	/* HERE BE QUEST CONSTANTS FOR THE HORDE QUEST */
	QUEST_FREE_FREEDOM_H		= 8000026,

	VOLK_SAY1					=  -2000001,		/* "Volkerdie"'s texts */
	VOLK_SAY2					=  -2000005,
	
	VOLK_EMO1					=  -2000000,	 	/* Volkerdie's txt emotes */
	
	CONN_SAY1					=  -2000003,		/* Connie Dobb's texts */
	CONN_SAY2					=  -2000004,
	CONN_SAY3					=  -2000006,
	
	CONN_EMO1					=  -2000002,		/* Connie Dobb's txt emotes */
	
	/* HERE BE QUEST CONSTANT FOR THE ALLIANCE QUEST */
	QUEST_FREE_FREEDOM_A		= 8000031,
	
	VOLK_SAY3					= -2000008,
	VOLK_SAY4					= -2000012,
	
	VOLK_EMO2					= -2000007,
	
	CONN_SAY4					= -2000010,
	CONN_SAY5					= -2000011,
	CONN_SAY6					= -2000013,
	
	CONN_EMO2					= -2000009
	};

class npc_free_as_in_freedom : public CreatureScript
{
public:
	npc_free_as_in_freedom() : CreatureScript("npc_free_as_in_freedom") { }

	bool ChooseReward(Player* /*pPlayer*/, Creature* pCreature, const Quest *_Quest, uint32 /*slot*/)
	{
		if (_Quest->GetQuestId() == 8000031 )
		{
			CAST_AI(npc_free_as_in_freedom::npc_free_as_in_freedom_endAI, pCreature->AI())->FAIF_A = true;
		}
		if (_Quest->GetQuestId() == 8000026 )
		{
			CAST_AI(npc_free_as_in_freedom::npc_free_as_in_freedom_endAI, pCreature->AI())->FAIF_H = true;
		}
		return true;
	}

	CreatureAI* GetAI(Creature* pCreature) const
	{
		return new npc_free_as_in_freedom_endAI (pCreature);
	}

struct npc_free_as_in_freedom_endAI : public ScriptedAI
{
	npc_free_as_in_freedom_endAI(Creature *pCreature) : ScriptedAI(pCreature) {}
	
	uint32 ExecuteSpeech_Timer;
	uint32 ExecuteSpeech_Counter;
	uint64 PlayerGUID;
	bool FAIF_H,
		FAIF_A;

	void Reset()
	{
		ExecuteSpeech_Timer = 0;
		ExecuteSpeech_Counter = 0;
		PlayerGUID = 0;
		FAIF_H = false;
		FAIF_A = false;

		me->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_OOC_NOT_ATTACKABLE);
	}

	void UpdateAI(const uint32 diff)
	{
		if (FAIF_H)
		{				
			if (ExecuteSpeech_Timer <= diff)
			{
				
					switch (ExecuteSpeech_Counter)
					{
						case 0: DoScriptText(VOLK_EMO1, me); ++ExecuteSpeech_Counter; break; 
						case 1: DoScriptText(VOLK_SAY1, me); ++ExecuteSpeech_Counter; break;
						case 2: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_EMO1, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 3: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY1, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 4: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY2, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 5: DoScriptText(VOLK_SAY2, me); ++ExecuteSpeech_Counter; break;
						case 6: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY3, pConnie); 
							++ExecuteSpeech_Counter; 
							FAIF_H = false;
							break;
					}
			
			if (ExecuteSpeech_Counter >=5)
				ExecuteSpeech_Timer = 4500;
			else
				ExecuteSpeech_Timer = 7000;
			}
			else
			ExecuteSpeech_Timer -= diff;

		}
		else
		{
			if (FAIF_A)
		{
			if (ExecuteSpeech_Timer <= diff)
			{
				
					switch (ExecuteSpeech_Counter)
					{
						case 0: DoScriptText(VOLK_EMO2, me); ++ExecuteSpeech_Counter; break; 
						case 1: DoScriptText(VOLK_SAY3, me); ++ExecuteSpeech_Counter; break;
						case 2: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_EMO2, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 3: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY4, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 4: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY5, pConnie); 
							++ExecuteSpeech_Counter; break;
						case 5: DoScriptText(VOLK_SAY4, me); ++ExecuteSpeech_Counter; break;
						case 6: 
							if (Creature* pConnie = GetClosestCreatureWithEntry(me, NPC_CONNIE, 10.0f))
								DoScriptText(CONN_SAY6, pConnie); 
							++ExecuteSpeech_Counter; 
							FAIF_A = false;
							break;
					}
			
			if (ExecuteSpeech_Counter >=5)
				ExecuteSpeech_Timer = 4500;
			else
				ExecuteSpeech_Timer = 7000;
			}
			else
			ExecuteSpeech_Timer -= diff;

		}
		}
	}
}; // End of class
}; // End of struct

void AddSC_free_as_in_freedom()
{
	new npc_free_as_in_freedom();
}
